﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetDataTableLatestDataHistoryList", "1.0")]
    public class GetDataTableLatestDataHistoryListAction : BaseAction<GetDataTableLatestDataHistoryListParams, GetDataTableLatestDataHistoryListResult>
    {
        protected override ActionResult<GetDataTableLatestDataHistoryListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetDataTableLatestDataHistoryListParams opParams)
        {
            SER.DtTableService service = new SER.DtTableService();
            ListResult<VIW.VDtTableDataHistory> serResult = service.GetHistories(opParams.DataTableId);
            if (serResult.IsSuccess)
            {
                GetDataTableLatestDataHistoryListResult actResult = new GetDataTableLatestDataHistoryListResult();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetDataTableLatestDataHistoryListResult.Item item = new GetDataTableLatestDataHistoryListResult.Item();

                    item.DataId = serResult.Data[index].DataId;
                    item.ColumnName = serResult.Data[index].ColumnName;

                    actResult.Items.Add(item);
                }

                return new ActionResult<GetDataTableLatestDataHistoryListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetDataTableLatestDataHistoryListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDataTableLatestDataHistoryListParams
    {
        [Parameter]
        public Guid DataTableId { get; set; }
    }

    public class GetDataTableLatestDataHistoryListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public string DataId { get; set; }

            public string ColumnName { get; set; }
        }
    }
}
